/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.milib.animation.dynamic_ {

	
	public class AnimatedInertialNumber extends AnimatedNumber {
		private var speed : Number;
		public var friction : Number;
		public var acceleration : Number;
		
		/**
		 * @param $acceleration biger - faster, delault:0.5		 * @param $friction smaller - longer animation, delault:0.1
		 */
		public function AnimatedInertialNumber(initialCurrentValue:Number=0, acceleration:Number=.5, friction:Number=.1) {
			super(initialCurrentValue);
			this.acceleration=acceleration;			this.friction=friction;
		}//<>
		
		override protected function doPrepare():void {
			speed=0;
		}
		
		override protected function checkIsFinish():Boolean {
			return Math.abs(target-current)<0.01 && Math.abs(speed)<0.001;
		}
		
		public function setSpeed(speed:Number):void {
			this.speed=speed;
		}
		
		public function getCurrentSpeed():Number {
			return speed;
		}
		
//****************************************************************************
// EVENTS for AnimatedInertialNumber
//****************************************************************************
		override protected function doEnterFrame():void {
			speed+=(target-current)*acceleration;
			speed-=speed*friction;
			current+=speed;
		}
	
	}
}